/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import GUI.tableTools.TableMaker;
import java.awt.Color;
import java.awt.Dimension;
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.filechooser.FileFilter;

/**
 * Fenêtres d'options pour l'application
 * @author isen
 */
public class OptionsFrame extends javax.swing.JFrame {
    Preferences prefs;
    public static final String MAX_TIME = "maxTime";
    public static final String SLOPES_ACCESS_PATH = "slopesPath";
    boolean maxTimeVerifier = true;
    
    
    /**
     * Creates new form OptionsFrame
     */
  
    public OptionsFrame() {
       
        
        initComponents();
        this.setPreferredSize(new Dimension(200,200));
        this.setVisible(true);
    }
    
    
    public OptionsFrame(Preferences prefs) {
       
        this.prefs = prefs;
        initComponents();
        setPreferences();
        this.setPreferredSize(new Dimension(200,200));
        this.setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        apply = new javax.swing.JButton();
        ok = new javax.swing.JButton();
        cancel = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        maxTime = new javax.swing.JPanel();
        maxTimeTimer = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        saveDirectory = new javax.swing.JPanel();
        saveDirectoryText = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        negSlopeText = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        posSlopeText = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        legalContent = new javax.swing.JTextArea();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        buttonSlope1 = new javax.swing.JButton();
        slopePath1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        buttonSlope2 = new javax.swing.JButton();
        slopePath2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        slopePath3 = new javax.swing.JTextField();
        buttonSlope3 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        slopePath4 = new javax.swing.JTextField();
        buttonSlope4 = new javax.swing.JButton();
        openEditorWithNewSlope = new javax.swing.JButton();
        openEditorWithExistingSlope = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        coefficients = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        cfmText = new javax.swing.JFormattedTextField(NumberFormat.FRACTION_FIELD);
        jLabel6 = new javax.swing.JLabel();
        cfaText = new javax.swing.JFormattedTextField(NumberFormat.FRACTION_FIELD);
        parametres = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        weightText = new javax.swing.JFormattedTextField(NumberFormat.INTEGER_FIELD);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Options");
        setName("options");
        setType(java.awt.Window.Type.POPUP);

        apply.setText("Apply");
        apply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                applyActionPerformed(evt);
            }
        });

        ok.setText("OK");
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });

        cancel.setText("Cancel");
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ok)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cancel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(apply)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {apply, cancel, ok});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(apply)
                    .addComponent(cancel)
                    .addComponent(ok))
                .addContainerGap())
        );

        maxTime.setBorder(javax.swing.BorderFactory.createTitledBorder("Limite d'utilisation"));

        maxTimeTimer.setText("0");
        maxTimeTimer.setToolTipText("limite en mètres. -1 : pas de limite");
        maxTimeTimer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                maxTimeTimerActionPerformed(evt);
            }
        });
        maxTimeTimer.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                maxTimeTimerFocusLost(evt);
            }
        });
        maxTimeTimer.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                maxTimeTimerKeyTyped(evt);
            }
        });

        jLabel9.setText("m");

        jCheckBox1.setSelected(prefs.getBoolean("testMode", false));
        jCheckBox1.setText("Mode Test");
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout maxTimeLayout = new javax.swing.GroupLayout(maxTime);
        maxTime.setLayout(maxTimeLayout);
        maxTimeLayout.setHorizontalGroup(
            maxTimeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(maxTimeLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(maxTimeTimer, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel9)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 218, Short.MAX_VALUE)
                .addComponent(jCheckBox1)
                .addGap(17, 17, 17))
        );
        maxTimeLayout.setVerticalGroup(
            maxTimeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, maxTimeLayout.createSequentialGroup()
                .addContainerGap(11, Short.MAX_VALUE)
                .addGroup(maxTimeLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(maxTimeTimer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(jCheckBox1))
                .addContainerGap())
        );

        saveDirectory.setBorder(javax.swing.BorderFactory.createTitledBorder("Dossier de sauvegarde des données utilisateur"));

        saveDirectoryText.setText(prefs.get("contactsDirectory", "D://Documents//contacts.txt"));
        saveDirectoryText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveDirectoryTextActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout saveDirectoryLayout = new javax.swing.GroupLayout(saveDirectory);
        saveDirectory.setLayout(saveDirectoryLayout);
        saveDirectoryLayout.setHorizontalGroup(
            saveDirectoryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(saveDirectoryLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(saveDirectoryText)
                .addContainerGap())
        );
        saveDirectoryLayout.setVerticalGroup(
            saveDirectoryLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(saveDirectoryText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Pentes maximales"));

        jLabel10.setText("de");

        negSlopeText.setText(prefs.get("negSlope", "-20")
        );
        negSlopeText.setToolTipText("Pente negative maximale admissible");
        negSlopeText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                negSlopeTextActionPerformed(evt);
            }
        });

        jLabel11.setText("% à ");

        posSlopeText.setText(prefs.get("posSlope", "20"));
        posSlopeText.setToolTipText("pente positive maximale admissible");
        posSlopeText.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                posSlopeTextActionPerformed(evt);
            }
        });

        jLabel12.setText("%");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(negSlopeText, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(posSlopeText, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel12)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(negSlopeText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11)
                    .addComponent(posSlopeText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("contenu du cadre juridique"));

        legalContent.setColumns(20);
        legalContent.setRows(5);
        jScrollPane1.setViewportView(legalContent);
        legalContent.setText(prefs.get("legalContent", "contenu du cadre légal de l'application"));

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 11, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(saveDirectory, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(maxTime, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(maxTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(saveDirectory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("utilisation", jPanel2);

        jLabel1.setText("Emplacement:");

        buttonSlope1.setText("Parcourir");
        buttonSlope1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSlope1ActionPerformed(evt);
            }
        });

        slopePath1.setText(prefs.get(SLOPES_ACCESS_PATH+1, "not set yet"));
        slopePath1.setToolTipText("Chemin d'accès pour les circuits prédéfinis");
        slopePath1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                slopePath1ActionPerformed(evt);
            }
        });

        jLabel2.setText("circuit 1 :");

        buttonSlope2.setText("Parcourir");
        buttonSlope2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSlope2ActionPerformed(evt);
            }
        });

        slopePath2.setText(prefs.get(SLOPES_ACCESS_PATH+2, "not set yet"));
        slopePath2.setToolTipText("Chemin d'accès pour les circuits prédéfinis");
        slopePath2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                slopePath2ActionPerformed(evt);
            }
        });

        jLabel3.setText("circuit 2:");

        jLabel4.setText("circuit 3:");

        slopePath3.setText(prefs.get(SLOPES_ACCESS_PATH+3, "not set yet"));
        slopePath3.setToolTipText("Chemin d'accès pour les circuits prédéfinis");
        slopePath3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                slopePath3ActionPerformed(evt);
            }
        });

        buttonSlope3.setText("Parcourir");
        buttonSlope3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSlope3ActionPerformed(evt);
            }
        });

        jLabel5.setText("circuit 4:");

        slopePath4.setText(prefs.get(SLOPES_ACCESS_PATH+4, "not set yet"));
        slopePath4.setToolTipText("Chemin d'accès pour les circuits prédéfinis");
        slopePath4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                slopePath4ActionPerformed(evt);
            }
        });

        buttonSlope4.setText("Parcourir");
        buttonSlope4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSlope4ActionPerformed(evt);
            }
        });

        openEditorWithNewSlope.setText("créer un circuit");
        openEditorWithNewSlope.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openEditorWithNewSlopeActionPerformed(evt);
            }
        });

        openEditorWithExistingSlope.setText("modifier un circuit");
        openEditorWithExistingSlope.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openEditorWithExistingSlopeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(slopePath4, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(buttonSlope4))
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel3Layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addGap(28, 28, 28)
                            .addComponent(slopePath1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(buttonSlope1))
                        .addComponent(jLabel1)
                        .addComponent(jLabel3))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(slopePath2, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(slopePath3, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(buttonSlope2)
                            .addComponent(buttonSlope3))))
                .addContainerGap(91, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(openEditorWithExistingSlope, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(openEditorWithNewSlope, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(slopePath1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonSlope1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(slopePath2, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonSlope2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(slopePath3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonSlope3)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(slopePath4, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonSlope4)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 163, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(openEditorWithNewSlope)
                    .addComponent(openEditorWithExistingSlope))
                .addContainerGap())
        );

        jTabbedPane1.addTab("circuits", jPanel3);

        coefficients.setBorder(javax.swing.BorderFactory.createTitledBorder("Coefficients de frottement"));

        jLabel7.setText("Frottement de l'air");

        cfmText.setText(Double.toString(prefs.getDouble("cfm", 0.04)));
        cfmText.setMinimumSize(new java.awt.Dimension(100, 20));
        cfmText.setPreferredSize(new java.awt.Dimension(100, 20));

        jLabel6.setText("Frottement mécanique");

        cfaText.setText(Double.toString(prefs.getDouble("cfa", 0.3)));
        cfaText.setMinimumSize(new java.awt.Dimension(100, 20));
        cfaText.setPreferredSize(new java.awt.Dimension(100, 20));

        javax.swing.GroupLayout coefficientsLayout = new javax.swing.GroupLayout(coefficients);
        coefficients.setLayout(coefficientsLayout);
        coefficientsLayout.setHorizontalGroup(
            coefficientsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(coefficientsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(coefficientsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(coefficientsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cfaText, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cfmText, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        coefficientsLayout.setVerticalGroup(
            coefficientsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(coefficientsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(coefficientsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(cfmText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(coefficientsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(cfaText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        parametres.setBorder(javax.swing.BorderFactory.createTitledBorder("parametres"));

        jLabel8.setText("Poid (cycliste+velo)");

        weightText.setText(Integer.toString(prefs.getInt("weight", 90)));
        weightText.setMinimumSize(new java.awt.Dimension(100, 20));
        weightText.setPreferredSize(new java.awt.Dimension(100, 20));

        javax.swing.GroupLayout parametresLayout = new javax.swing.GroupLayout(parametres);
        parametres.setLayout(parametresLayout);
        parametresLayout.setHorizontalGroup(
            parametresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(parametresLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(weightText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        parametresLayout.setVerticalGroup(
            parametresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(parametresLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel8)
                .addComponent(weightText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(parametres, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(coefficients, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(165, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(coefficients, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(parametres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(202, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Puissance", jPanel4);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void applyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_applyActionPerformed
        commitPreferences();
    }//GEN-LAST:event_applyActionPerformed

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_cancelActionPerformed

    private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
        if(commitPreferences()) this.dispose();
    }//GEN-LAST:event_okActionPerformed

    private void maxTimeTimerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_maxTimeTimerActionPerformed
        
        try{
        if (Integer.parseInt(maxTimeTimer.getText())>=-1){
        maxTimeTimer.setBackground(Color.white);
        this.maxTimeVerifier = true;
        return;
        }
        maxTimeTimer.setToolTipText("La limite doit être égale à -1 <br> ou strictement supérieure à 0");
        } catch (NumberFormatException ex){
            
            maxTimeTimer.setToolTipText("veuillez n'entrer que des chiffres");
        }
        maxTimeTimer.setBackground(Color.red);
        this.maxTimeVerifier = false;
    }//GEN-LAST:event_maxTimeTimerActionPerformed

    private void buttonSlope1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSlope1ActionPerformed
        this.slopePath1.setText(browseFile(1));
        
        
    }//GEN-LAST:event_buttonSlope1ActionPerformed

    private void slopePath1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_slopePath1ActionPerformed
          
    }//GEN-LAST:event_slopePath1ActionPerformed

    private void buttonSlope2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSlope2ActionPerformed
        this.slopePath2.setText(browseFile(2));
    }//GEN-LAST:event_buttonSlope2ActionPerformed

    private void slopePath2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_slopePath2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_slopePath2ActionPerformed

    private void slopePath3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_slopePath3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_slopePath3ActionPerformed

    private void buttonSlope3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSlope3ActionPerformed
       this.slopePath3.setText(browseFile(3));
    }//GEN-LAST:event_buttonSlope3ActionPerformed

    private void slopePath4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_slopePath4ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_slopePath4ActionPerformed

    private void buttonSlope4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSlope4ActionPerformed
        this.slopePath4.setText(browseFile(4));
    }//GEN-LAST:event_buttonSlope4ActionPerformed

    private void openEditorWithNewSlopeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openEditorWithNewSlopeActionPerformed
        TableMaker maker = new TableMaker();
       
    }//GEN-LAST:event_openEditorWithNewSlopeActionPerformed

    private void openEditorWithExistingSlopeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openEditorWithExistingSlopeActionPerformed
        JFileChooser chooser = new JFileChooser(prefs.get("path", "C://Documents"));
        
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.addChoosableFileFilter(new FileFilter() {

            @Override
            public boolean accept(File file) {
                String name = file.getName();
                
                if(!name.endsWith(".txt")){
                 System.out.println("illegal file format");
                 return false;
            }
                return true;
            }

            @Override
            public String getDescription() {
                return "*.txt files";
            }
        });
        
        int retour = chooser.showDialog(this, "choisir");
        if(retour == JFileChooser.APPROVE_OPTION){
            try {
                prefs.put("path", chooser.getSelectedFile().getParent());
                TableMaker maker = new TableMaker(chooser.getSelectedFile());
            } catch (IOException ex) {
                Logger.getLogger(OptionsFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_openEditorWithExistingSlopeActionPerformed

    private void maxTimeTimerKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_maxTimeTimerKeyTyped
        this.maxTimeTimerActionPerformed(null);
    }//GEN-LAST:event_maxTimeTimerKeyTyped

    private void maxTimeTimerFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_maxTimeTimerFocusLost
        this.maxTimeTimerActionPerformed(null);
    }//GEN-LAST:event_maxTimeTimerFocusLost

    private void saveDirectoryTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveDirectoryTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_saveDirectoryTextActionPerformed

    private void negSlopeTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_negSlopeTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_negSlopeTextActionPerformed

    private void posSlopeTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_posSlopeTextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_posSlopeTextActionPerformed

    private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
       
    }//GEN-LAST:event_jCheckBox1ActionPerformed

    /**
     * @param args the command line arguments
     */
    
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(OptionsFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(OptionsFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(OptionsFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(OptionsFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new OptionsFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton apply;
    private javax.swing.JButton buttonSlope1;
    private javax.swing.JButton buttonSlope2;
    private javax.swing.JButton buttonSlope3;
    private javax.swing.JButton buttonSlope4;
    private javax.swing.JButton cancel;
    private javax.swing.JFormattedTextField cfaText;
    private javax.swing.JFormattedTextField cfmText;
    private javax.swing.JPanel coefficients;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextArea legalContent;
    private javax.swing.JPanel maxTime;
    private javax.swing.JTextField maxTimeTimer;
    private javax.swing.JTextField negSlopeText;
    private javax.swing.JButton ok;
    private javax.swing.JButton openEditorWithExistingSlope;
    private javax.swing.JButton openEditorWithNewSlope;
    private javax.swing.JPanel parametres;
    private javax.swing.JTextField posSlopeText;
    private javax.swing.JPanel saveDirectory;
    private javax.swing.JTextField saveDirectoryText;
    private javax.swing.JTextField slopePath1;
    private javax.swing.JTextField slopePath2;
    private javax.swing.JTextField slopePath3;
    private javax.swing.JTextField slopePath4;
    private javax.swing.JFormattedTextField weightText;
    // End of variables declaration//GEN-END:variables

    
    private boolean commitPreferences() {
        
        this.maxTimeTimerActionPerformed(null);
        if(this.maxTimeVerifier){
            prefs.put("maxTime", maxTimeTimer.getText());
        } else {
            JOptionPane.showMessageDialog(this,"impossible de sauvegarder la durée maximum: <br>veuillez entrer un entier supérieur ou égal à -1");
            return false;
        }

            prefs.put(OptionsFrame.SLOPES_ACCESS_PATH+1, this.slopePath1.getText());
            prefs.put(OptionsFrame.SLOPES_ACCESS_PATH+2, this.slopePath2.getText());
            prefs.put(OptionsFrame.SLOPES_ACCESS_PATH+3, this.slopePath3.getText());
            prefs.put(OptionsFrame.SLOPES_ACCESS_PATH+4, this.slopePath4.getText());
        
        prefs.putDouble("cfa", Double.parseDouble(cfaText.getText()));
        prefs.putDouble("cfm", Double.parseDouble(cfmText.getText()));
        prefs.putInt("weight", Integer.parseInt(weightText.getText()));
        prefs.put("contactsDirectory", this.saveDirectoryText.getText());
        
        try{
        Integer.parseInt(this.negSlopeText.getText());
        this.negSlopeText.setBackground(Color.white);
        prefs.put("negSlope", this.negSlopeText.getText());
        }catch (NumberFormatException ex){
            this.negSlopeText.setBackground(Color.red);
            JOptionPane.showMessageDialog(this,"La pente minimale doit être un entier!");
            return false;
        }
        try{
        Integer.parseInt(this.posSlopeText.getText());
        this.negSlopeText.setBackground(Color.white);
        prefs.put("posSlope", this.posSlopeText.getText());
        }catch (NumberFormatException ex){
            this.posSlopeText.setBackground(Color.red);
            JOptionPane.showMessageDialog(this,"La pente maximale doit être un entier!");
            return false;
        }
        
        prefs.put("legalContent", this.legalContent.getText());
       
        
        prefs.putBoolean("testMode", jCheckBox1.isSelected());
        
        return true;
    }

    private void setPreferences() {
        
        this.maxTimeTimer.setText(prefs.get(MAX_TIME, "0"));
        
    }
    
    private String browseFile(int i){
    JFileChooser chooser = new JFileChooser(prefs.get(OptionsFrame.SLOPES_ACCESS_PATH+i,"C:\\Documents"));
        int retour = chooser.showDialog(this, "ok");
        if(retour == JFileChooser.APPROVE_OPTION){
            //prefs.put(OptionsFrame.SLOPES_ACCESS_PATH+i,chooser.getSelectedFile().getAbsolutePath());
            return chooser.getSelectedFile().getAbsolutePath();

        }
        return prefs.get(OptionsFrame.SLOPES_ACCESS_PATH+i,"D:\\Documents");
    }
}
